Load all required libraries.

library(tidyverse)
## -- Attaching packages --------------------------------------- tidyverse 1.3.1 --
## v ggplot2 3.3.5     v purrr   0.3.4
## v tibble  3.1.6     v dplyr   1.0.8
## v tidyr   1.2.0     v stringr 1.4.0
## v readr   2.1.2     v forcats 0.5.1
## -- Conflicts ------------------------------------------ tidyverse_conflicts() --
## x dplyr::filter() masks stats::filter()
## x dplyr::lag()    masks stats::lag()
library(plotly)
## 
## Attaching package: 'plotly'
## The following object is masked from 'package:ggplot2':
## 
##     last_plot
## The following object is masked from 'package:stats':
## 
##     filter
## The following object is masked from 'package:graphics':
## 
##     layout
library(broom)

Read in raw data from RDS.

raw_data <- readRDS("./year2.RDS")

Make a few small modifications to names and data for visualizations.

final_data <- raw_data %>% mutate(log_copy_per_L = log10(mean_copy_num_L)) %>%
  rename(Facility = wrf) %>%
  mutate(Facility = recode(Facility, 
                           "NO" = "WRF A",
                           "MI" = "WRF B",
                           "CC" = "WRF C"))

Seperate the data by gene target to ease layering in the final plot

#make three data layers
only_positives <<- subset(final_data, (!is.na(final_data$Facility)))
only_n1 <- subset(only_positives, target == "N1")
only_n2 <- subset(only_positives, target == "N2")
only_background <<-final_data %>% 
  select(c(date, cases_cum_clarke, new_cases_clarke, X7_day_ave_clarke)) %>%
  group_by(date) %>% summarise_if(is.numeric, mean)

#specify fun colors
background_color <- "#7570B3"
seven_day_ave_color <- "#E6AB02"
marker_colors <- c("N1" = '#1B9E77',"N2" ='#D95F02')
#remove facilty C for now
#only_n1 <- only_n1[!(only_n1$Facility == "WRF C"),]
#only_n2 <- only_n2[!(only_n2$Facility == "WRF C"),]

only_n1 <- only_n1[!(only_n1$Facility == "WRF A" & only_n1$date == "2020-11-02"), ]
only_n2 <- only_n2[!(only_n2$Facility == "WRF A" & only_n2$date == "2020-11-02"), ]

Build the main plot

      #first layer is the background epidemic curve
        p1 <- only_background %>%
              plotly::plot_ly() %>%
              plotly::add_trace(x = ~date, y = ~new_cases_clarke, 
                                type = "bar", 
                                hoverinfo = "text",
                                text = ~paste('</br> Date: ', date,
                                                     '</br> Daily Cases: ', new_cases_clarke),
                                alpha = 0.5,
                                name = "Daily Reported Cases",
                                color = background_color,
                                colors = background_color,
                                showlegend = FALSE) %>%
            layout(yaxis = list(title = "Clarke County Daily Cases", showline=TRUE)) %>%
            layout(legend = list(orientation = "h", x = 0.2, y = -0.3))
        
        #renders the main plot layer two as seven day moving average
        p1 <- p1 %>% plotly::add_trace(x = ~date, y = ~X7_day_ave_clarke, 
                             type = "scatter",
                             mode = "lines",
                             hoverinfo = "text",
                            text = ~paste('</br> Date: ', date,
                                                     '</br> Seven-Day Moving Average: ', X7_day_ave_clarke),
                             name = "Seven Day Moving Average Athens",
                             line = list(color = seven_day_ave_color),
                             showlegend = FALSE)
      

        
        #renders the main plot layer three as positive target hits
        
        p2 <- plotly::plot_ly() %>%
          plotly::add_trace(x = ~date, y = ~mean_copy_num_L,
                                       type = "scatter",
                                       mode = "markers",
                                       hoverinfo = "text",
                                       text = ~paste('</br> Date: ', date,
                                                     '</br> Facility: ', Facility,
                                                     '</br> Target: ', target,
                                                     '</br> Copies/L: ', round(mean_copy_num_L, digits = 2)),
                                       data = only_n1,
                                       symbol = ~Facility,
                                       marker = list(color = '#1B9E77', size = 8, opacity = 0.65),
                                       showlegend = FALSE) %>%
          plotly::add_trace(x = ~date, y = ~mean_copy_num_L,
                                       type = "scatter",
                                       mode = "markers",
                                       hoverinfo = "text",
                                       text = ~paste('</br> Date: ', date,
                                                     '</br> Facility: ', Facility,
                                                     '</br> Target: ', target,
                                                     '</br> Copies/L: ', round(mean_copy_num_L, digits = 2)),
                                       data = only_n2,
                                       symbol = ~Facility,
                                       marker = list(color = '#D95F02', size = 8, opacity = 0.65),
                                       showlegend = FALSE) %>%
            layout(yaxis = list(title = "SARS CoV-2 Copies/L", 
                                 showline = TRUE,
                                 type = "log",
                                 dtick = 1,
                                 automargin = TRUE)) %>%
            layout(legend = list(orientation = "h", x = 0.2, y = -0.3))
        
        #adds the limit of detection dashed line
        p2 <- p2 %>% plotly::add_segments(x = as.Date("2021-06-30"), 
                                          xend = ~max(date + 10), 
                                          y = 3571.429, yend = 3571.429,
                                          opacity = 0.35,
                                          line = list(color = "black", dash = "dash")) %>%
          layout(annotations = list(x = as.Date("2021-06-30"), y = 3.8, xref = "x", yref = "y", 
                                    text = "Limit of Detection", showarrow = FALSE))

        

        p1
        p2

Combine the two main plot pieces as a subplot

#seperate n1 and n2 frames by site
#n1
wrf_a_only_n1 <- subset(only_n1, Facility == "WRF A")
wrf_b_only_n1 <- subset(only_n1, Facility == "WRF B")
wrf_c_only_n1 <- subset(only_n1, Facility == "WRF C")

#n2
wrf_a_only_n2 <- subset(only_n2, Facility == "WRF A")
wrf_b_only_n2 <- subset(only_n2, Facility == "WRF B")
wrf_c_only_n2 <- subset(only_n2, Facility == "WRF C")


#rejoin the old data frames then seperate in to averages for each plant. 
wrfa_both <- full_join(wrf_a_only_n1, wrf_a_only_n2)%>%
  select(c(date, mean_total_copies)) %>%
  group_by(date) %>%
  summarize_if(is.numeric, mean) %>%
  ungroup() %>%
  mutate(log_total_copies_both = log10(mean_total_copies))
## Joining, by = c("date", "new_cases_clarke", "cases_cum_clarke",
## "X7_day_ave_clarke", "Facility", "collection_num", "target",
## "mean_copy_num_uL_rxn", "mean_copy_num_L", "sd_L", "se_L", "mean_total_copies",
## "sd_total_copies", "lo_95", "up_95", "log_copy_per_L")
wrfb_both <- full_join(wrf_b_only_n1, wrf_b_only_n2)%>%
  select(c(date, mean_total_copies)) %>%
  group_by(date) %>%
  summarize_if(is.numeric, mean) %>%
  ungroup() %>%
  mutate(log_total_copies_both = log10(mean_total_copies))
## Joining, by = c("date", "new_cases_clarke", "cases_cum_clarke",
## "X7_day_ave_clarke", "Facility", "collection_num", "target",
## "mean_copy_num_uL_rxn", "mean_copy_num_L", "sd_L", "se_L", "mean_total_copies",
## "sd_total_copies", "lo_95", "up_95", "log_copy_per_L")
wrfc_both <- full_join(wrf_c_only_n1, wrf_c_only_n2)%>%
  select(c(date, mean_total_copies)) %>%
  group_by(date) %>%
  summarize_if(is.numeric, mean) %>%
  ungroup() %>%
  mutate(log_total_copies_both = log10(mean_total_copies))
## Joining, by = c("date", "new_cases_clarke", "cases_cum_clarke",
## "X7_day_ave_clarke", "Facility", "collection_num", "target",
## "mean_copy_num_uL_rxn", "mean_copy_num_L", "sd_L", "se_L", "mean_total_copies",
## "sd_total_copies", "lo_95", "up_95", "log_copy_per_L")
#get max date
maxdate <- max(wrfa_both$date)
mindate <- min(wrfa_both$date)

Build loess smoothing figures figures

This makes the individual plots

#**************************************WRF A PLOT**********************************************
#add trendlines 
#extract data from geom_smooth
#both extract
# *********************************span 0.6***********************************
#*****************Must always update the n = TOTAL NUMBER OF DAYS*************************
extract_botha <- ggplot(wrfa_both, aes(x = date, y = log_total_copies_both)) + 
  stat_smooth(aes(outfit=fit_botha<<-..y..), method = "loess", color = '#1B9E77', 
              span = 0.25, n = 386)
## Warning: Ignoring unknown aesthetics: outfit
#look at the fits to align dates and total observations
#both
extract_botha
## `geom_smooth()` using formula 'y ~ x'

fit_botha
##   [1] 11.47183 11.51757 11.56264 11.60706 11.65086 11.69405 11.73665 11.77868
##   [9] 11.82016 11.86110 11.90153 11.94146 11.98092 12.01991 12.05847 12.09659
##  [17] 12.13426 12.17145 12.20811 12.24422 12.27974 12.31463 12.34887 12.38241
##  [25] 12.41522 12.44727 12.47852 12.50940 12.54028 12.57104 12.60155 12.63172
##  [33] 12.66141 12.69051 12.71891 12.74648 12.77312 12.79870 12.82310 12.84622
##  [41] 12.86793 12.88989 12.91337 12.93764 12.96197 12.98563 13.00789 13.02804
##  [49] 13.04534 13.05906 13.07070 13.08209 13.09305 13.10338 13.11291 13.12143
##  [57] 13.12877 13.13473 13.13912 13.14176 13.14246 13.14103 13.13728 13.13101
##  [65] 13.12077 13.10568 13.08650 13.06398 13.03888 13.01193 12.98388 12.95550
##  [73] 12.92752 12.90070 12.87579 12.85353 12.82886 12.79748 12.76124 12.72202
##  [81] 12.68169 12.64212 12.60516 12.57271 12.54662 12.52353 12.49909 12.47368
##  [89] 12.44767 12.42145 12.39540 12.36988 12.34529 12.32199 12.30038 12.28082
##  [97] 12.26369 12.24847 12.23435 12.22128 12.20922 12.19811 12.18792 12.17859
## [105] 12.17008 12.16234 12.15532 12.14898 12.14328 12.13815 12.13357 12.13091
## [113] 12.13118 12.13370 12.13784 12.14293 12.14833 12.15339 12.15745 12.15986
## [121] 12.16244 12.16716 12.17363 12.18142 12.19013 12.19936 12.20868 12.21770
## [129] 12.22599 12.23317 12.23880 12.24249 12.24495 12.24722 12.24936 12.25145
## [137] 12.25356 12.25576 12.25811 12.26070 12.26358 12.26607 12.26755 12.26820
## [145] 12.26823 12.26782 12.26716 12.26645 12.26588 12.26564 12.26592 12.26692
## [153] 12.26881 12.27181 12.27610 12.28186 12.28930 12.29860 12.30996 12.32356
## [161] 12.34030 12.36036 12.38287 12.40697 12.43182 12.45654 12.48028 12.50217
## [169] 12.52136 12.54163 12.56690 12.59649 12.62974 12.66598 12.70455 12.74478
## [177] 12.78601 12.82756 12.86878 12.90899 12.94753 12.98373 13.01693 13.04646
## [185] 13.07166 13.09185 13.10637 13.11456 13.11953 13.12452 13.12902 13.13253
## [193] 13.13453 13.13453 13.13202 13.12649 13.11744 13.10285 13.08161 13.05456
## [201] 13.02254 12.98640 12.94700 12.90517 12.86175 12.81760 12.77357 12.73049
## [209] 12.68921 12.65058 12.61545 12.57904 12.53677 12.48979 12.43923 12.38624
## [217] 12.33196 12.27752 12.22407 12.17274 12.12468 12.08102 12.04291 12.00709
## [225] 11.96981 11.93144 11.89238 11.85302 11.81373 11.77490 11.73691 11.70016
## [233] 11.66503 11.63190 11.60116 11.57319 11.54838 11.52437 11.49912 11.47350
## [241] 11.44839 11.42466 11.40319 11.38484 11.37050 11.35946 11.35041 11.34328
## [249] 11.33801 11.33454 11.33280 11.33273 11.33427 11.33736 11.34192 11.34791
## [257] 11.35525 11.36389 11.37376 11.38479 11.40000 11.42166 11.44856 11.47950
## [265] 11.51328 11.54870 11.58456 11.61967 11.65282 11.68282 11.70846 11.72855
## [273] 11.74771 11.77064 11.79624 11.82342 11.85107 11.87810 11.90340 11.92588
## [281] 11.94444 11.95983 11.97370 11.98634 11.99802 12.00901 12.01961 12.03008
## [289] 12.04070 12.05175 12.06350 12.07624 12.09025 12.10410 12.11635 12.12726
## [297] 12.13707 12.14601 12.15434 12.16229 12.17011 12.17805 12.18634 12.19524
## [305] 12.20498 12.21580 12.22796 12.24151 12.25622 12.27192 12.28842 12.30555
## [313] 12.32312 12.34096 12.35888 12.37671 12.39426 12.41137 12.42783 12.44349
## [321] 12.45815 12.47420 12.49348 12.51502 12.53787 12.56105 12.58359 12.60454
## [329] 12.62292 12.63777 12.64980 12.66049 12.66999 12.67847 12.68611 12.69306
## [337] 12.69950 12.70559 12.71150 12.71739 12.72344 12.72981 12.73601 12.74150
## [345] 12.74636 12.75068 12.75454 12.75802 12.76121 12.76420 12.76706 12.76987
## [353] 12.77274 12.77572 12.77892 12.78241 12.78588 12.78900 12.79180 12.79435
## [361] 12.79670 12.79888 12.80096 12.80297 12.80498 12.80689 12.80859 12.81009
## [369] 12.81139 12.81251 12.81346 12.81425 12.81488 12.81538 12.81575 12.81600
## [377] 12.81613 12.81618 12.81616 12.81604 12.81582 12.81547 12.81498 12.81434
## [385] 12.81354 12.81255
#assign fits to a vector
both_trenda <- fit_botha

#extract y min and max for each
limits_botha <- ggplot_build(extract_botha)$data
## `geom_smooth()` using formula 'y ~ x'
limits_botha <- as.data.frame(limits_botha)
both_ymina <- limits_botha$ymin
both_ymaxa <- limits_botha$ymax

#reassign dataframes (just to be safe)
work_botha <- wrfa_both

#fill in missing dates to smooth fits
work_botha <- work_botha %>% complete(date = seq(min(date), max(date), by = "1 day"))
date_vec_botha <- work_botha$date

#create a new smooth dataframe to layer
smooth_frame_botha <- data.frame(date_vec_botha, both_trenda, both_ymina, both_ymaxa)
#WRF A
#plot smooth frames
p_wrf_a <- plotly::plot_ly() %>%
  plotly::add_lines(x = ~date_vec_botha, y = ~both_trenda,
                    data = smooth_frame_botha,
                    hoverinfo = "text",
                    text = ~paste('</br> Date: ', date_vec_botha,
                                  '</br> Median Log Copies: ', round(both_trenda, digits = 2)),
                    line = list(color = '#1B9E77', size = 8, opacity = 0.65),
                    showlegend = FALSE) %>%
     layout(xaxis = list(range = c(mindate - 7, maxdate + 7))) %>% #buffer here
plotly::add_ribbons(x ~date_vec_botha, ymin = ~both_ymina, ymax = ~both_ymaxa,
                    showlegend = FALSE,
                    opacity = 0.25,
                    hoverinfo = "text",
                    text = ~paste('</br> Date: ', date_vec_botha, #leaving in case we want to change
                                  '</br> Max Log Copies: ', round(both_ymaxa, digits = 2),
                                  '</br> Min Log Copies: ', round(both_ymina, digits = 2)),
                    name = "",
                    fillcolor = '#1B9E77',
                    line = list(color = '#1B9E77')) %>%
                layout(yaxis = list(title = "Total Log10 SARS CoV-2 Copies", 
                                 showline = TRUE,
                                 automargin = TRUE)) %>%
                layout(xaxis = list(title = "Date")) %>%
                layout(title = "WRF A") %>%
  plotly::add_markers(x = ~date, y = ~log_total_copies_both,
                      data = wrfa_both,
                       hoverinfo = "text",
                       showlegend = FALSE,
                       text = ~paste('</br> Date: ', date, 
                                     '</br> Actual Log Copies: ', round(log_total_copies_both, digits = 2)),
                       marker = list(color = '#1B9E77', size = 6, opacity = 0.65))

p_wrf_a
save(p_wrf_a, file = "./site_objects/wrf_a_year2.rda")
#**************************************WRF B PLOT**********************************************
#add trendlines 
#extract data from geom_smooth
#both extract
# *********************************span 0.6***********************************
#*****************Must always update the n = TOTAL NUMBER OF DAYS*************************
extract_bothb <- ggplot(wrfb_both, aes(x = date, y = log_total_copies_both)) + 
  stat_smooth(aes(outfit=fit_bothb<<-..y..), method = "loess", color = '#D95F02', 
              span = 0.25, n = 386)
## Warning: Ignoring unknown aesthetics: outfit
#look at the fits to align dates and total observations
#both
extract_bothb
## `geom_smooth()` using formula 'y ~ x'

fit_bothb
##   [1] 10.76137 10.84827 10.93353 11.01715 11.09910 11.17936 11.25793 11.33479
##   [9] 11.40992 11.48331 11.55493 11.62478 11.69284 11.75909 11.82352 11.88612
##  [17] 11.94690 12.00589 12.06313 12.11865 12.17247 12.22463 12.27515 12.32407
##  [25] 12.37142 12.41723 12.46152 12.50401 12.54441 12.58281 12.61930 12.65394
##  [33] 12.68682 12.71803 12.74763 12.77572 12.80236 12.82765 12.85165 12.87446
##  [41] 12.89614 12.91507 12.93003 12.94180 12.95111 12.95872 12.96538 12.97185
##  [49] 12.97887 12.98720 12.99492 12.99983 13.00227 13.00258 13.00107 12.99809
##  [57] 12.99398 12.98906 12.98366 12.97813 12.97280 12.96800 12.96405 12.96131
##  [65] 12.95939 12.95761 12.95581 12.95382 12.95149 12.94866 12.94518 12.94088
##  [73] 12.93561 12.92921 12.92152 12.91238 12.90133 12.88830 12.87369 12.85794
##  [81] 12.84146 12.82468 12.80802 12.79189 12.77672 12.76255 12.74901 12.73584
##  [89] 12.72280 12.70965 12.69613 12.68201 12.66704 12.65097 12.63356 12.61457
##  [97] 12.59374 12.56901 12.53907 12.50481 12.46711 12.42686 12.38495 12.34227
## [105] 12.29970 12.25815 12.21849 12.18162 12.14841 12.11977 12.09658 12.07318
## [113] 12.04457 12.01261 11.97916 11.94608 11.91525 11.88853 11.86777 11.85484
## [121] 11.84810 11.84442 11.84342 11.84474 11.84803 11.85291 11.85902 11.86600
## [129] 11.87348 11.88110 11.88848 11.89528 11.90361 11.91541 11.92998 11.94665
## [137] 11.96472 11.98351 12.00234 12.02053 12.03738 12.05527 12.07676 12.10149
## [145] 12.12909 12.15919 12.19141 12.22540 12.26077 12.29717 12.33421 12.37153
## [153] 12.40876 12.44553 12.48148 12.51622 12.54939 12.58062 12.60955 12.63579
## [161] 12.66043 12.68472 12.70862 12.73206 12.75498 12.77732 12.79902 12.82001
## [169] 12.84025 12.86105 12.88360 12.90761 12.93279 12.95885 12.98552 13.01250
## [177] 13.03950 13.06624 13.09244 13.11780 13.14204 13.16488 13.18602 13.20518
## [185] 13.22207 13.23640 13.24790 13.25627 13.26384 13.27264 13.28187 13.29078
## [193] 13.29859 13.30452 13.30781 13.30768 13.30336 13.29484 13.28285 13.26769
## [201] 13.24969 13.22916 13.20642 13.18177 13.15554 13.12804 13.09959 13.07050
## [209] 13.04108 13.01166 12.98255 12.95047 12.91266 12.87014 12.82396 12.77513
## [217] 12.72471 12.67371 12.62319 12.57417 12.52768 12.48477 12.44646 12.40888
## [225] 12.36783 12.32392 12.27776 12.22995 12.18109 12.13179 12.08265 12.03427
## [233] 11.98726 11.94222 11.89975 11.86046 11.82495 11.79004 11.75290 11.71465
## [241] 11.67642 11.63935 11.60456 11.57318 11.54634 11.52270 11.50021 11.47895
## [249] 11.45897 11.44037 11.42320 11.40755 11.39348 11.38106 11.37037 11.36147
## [257] 11.35445 11.34937 11.34630 11.34532 11.34768 11.35420 11.36426 11.37725
## [265] 11.39254 11.40952 11.42758 11.44611 11.46448 11.48208 11.49830 11.51252
## [273] 11.52729 11.54513 11.56532 11.58717 11.60999 11.63308 11.65574 11.67728
## [281] 11.69700 11.71727 11.74058 11.76640 11.79420 11.82344 11.85360 11.88413
## [289] 11.91452 11.94422 11.97271 11.99946 12.02392 12.04738 12.07137 12.09576
## [297] 12.12042 12.14521 12.17000 12.19467 12.21907 12.24308 12.26657 12.28939
## [305] 12.31143 12.33255 12.35262 12.37199 12.39113 12.41000 12.42860 12.44692
## [313] 12.46494 12.48265 12.50004 12.51709 12.53379 12.55013 12.56609 12.58167
## [321] 12.59684 12.61244 12.62906 12.64633 12.66388 12.68135 12.69837 12.71458
## [329] 12.72961 12.74309 12.75467 12.76443 12.77266 12.77965 12.78567 12.79102
## [337] 12.79598 12.80084 12.80588 12.81140 12.81766 12.82497 12.83265 12.83989
## [345] 12.84676 12.85333 12.85967 12.86586 12.87196 12.87804 12.88417 12.89043
## [353] 12.89689 12.90361 12.91066 12.91812 12.92573 12.93320 12.94058 12.94791
## [361] 12.95523 12.96259 12.97003 12.97759 12.98532 12.99314 13.00096 13.00878
## [369] 13.01661 13.02446 13.03234 13.04025 13.04820 13.05619 13.06424 13.07234
## [377] 13.08052 13.08878 13.09714 13.10559 13.11411 13.12269 13.13132 13.13999
## [385] 13.14868 13.15739
#assign fits to a vector
both_trendb <- fit_bothb

#extract y min and max for each
limits_bothb <- ggplot_build(extract_bothb)$data
## `geom_smooth()` using formula 'y ~ x'
limits_bothb <- as.data.frame(limits_bothb)
both_yminb <- limits_bothb$ymin
both_ymaxb <- limits_bothb$ymax

#reassign dataframes (just to be safe)
work_bothb <- wrfb_both

#fill in missing dates to smooth fits
work_bothb <- work_bothb %>% complete(date = seq(min(date), max(date), by = "1 day"))
date_vec_bothb <- work_bothb$date

#create a new smooth dataframe to layer
smooth_frame_bothb <- data.frame(date_vec_bothb, both_trendb, both_yminb, both_ymaxb)
#WRF B
#plot smooth frames
p_wrf_b <- plotly::plot_ly() %>%
  plotly::add_lines(x = ~date_vec_bothb, y = ~both_trendb,
                    data = smooth_frame_bothb,
                    hoverinfo = "text",
                    text = ~paste('</br> Date: ', date_vec_bothb,
                                  '</br> Median Log Copies: ', round(both_trendb, digits = 2)),
                    line = list(color = '#D95F02', size = 8, opacity = 0.65),
                    showlegend = FALSE) %>%
     layout(xaxis = list(range = c(mindate - 7, maxdate + 7))) %>% #buffer here
plotly::add_ribbons(x ~date_vec_bothb, ymin = ~both_yminb, ymax = ~both_ymaxb,
                    showlegend = FALSE,
                    opacity = 0.25,
                    hoverinfo = "text",
                    text = ~paste('</br> Date: ', date_vec_bothb, #leaving in case we want to change
                                  '</br> Max Log Copies: ', round(both_ymaxb, digits = 2),
                                  '</br> Min Log Copies: ', round(both_yminb, digits = 2)),
                    name = "",
                    fillcolor = '#D95F02',
                    line = list(color = '#D95F02')) %>%
                layout(yaxis = list(title = "Total Log10 SARS CoV-2 Copies", 
                                 showline = TRUE,
                                 automargin = TRUE)) %>%
                layout(xaxis = list(title = "Date")) %>%
                layout(title = "WRF B") %>%
  plotly::add_markers(x = ~date, y = ~log_total_copies_both,
                      data = wrfb_both,
                       hoverinfo = "text",
                       showlegend = FALSE,
                       text = ~paste('</br> Date: ', date, 
                                     '</br> Actual Log Copies: ', round(log_total_copies_both, digits = 2)),
                       marker = list(color = '#D95F02', size = 6, opacity = 0.65))

p_wrf_b
save(p_wrf_b, file = "./site_objects/wrf_b_year2.rda")

#**************************************WRF C PLOT********************************************** #add trendlines #extract data from geom_smooth # *********************************span 0.6*********************************** #*****************Must always update the n = TOTAL NUMBER OF DAYS*************************

extract_bothc <- ggplot(wrfc_both, aes(x = date, y = log_total_copies_both)) + 
  stat_smooth(aes(outfit=fit_bothc<<-..y..), method = "loess", color = '#E7298A', 
              span = 0.25, n = 386)
## Warning: Ignoring unknown aesthetics: outfit
#look at the fits to align dates and total observations
#both
extract_bothc
## `geom_smooth()` using formula 'y ~ x'

fit_bothc
##   [1] 10.85738 10.91759 10.97678 11.03495 11.09207 11.14814 11.20316 11.25711
##   [9] 11.30997 11.36175 11.41243 11.46199 11.51044 11.55775 11.60393 11.64900
##  [17] 11.69303 11.73600 11.77793 11.81880 11.85864 11.89743 11.93518 11.97188
##  [25] 12.00755 12.04218 12.07578 12.10805 12.13878 12.16806 12.19597 12.22261
##  [33] 12.24806 12.27243 12.29581 12.31827 12.33993 12.36085 12.38115 12.40091
##  [41] 12.42021 12.43945 12.45876 12.47788 12.49655 12.51450 12.53149 12.54724
##  [49] 12.56149 12.57399 12.58522 12.59581 12.60571 12.61487 12.62325 12.63078
##  [57] 12.63743 12.64315 12.64789 12.65159 12.65422 12.65572 12.65604 12.65514
##  [65] 12.65268 12.64849 12.64273 12.63558 12.62721 12.61777 12.60745 12.59641
##  [73] 12.58483 12.57286 12.56068 12.54847 12.53353 12.51387 12.49073 12.46531
##  [81] 12.43884 12.41254 12.38762 12.36531 12.34682 12.32917 12.30895 12.28671
##  [89] 12.26305 12.23853 12.21372 12.18919 12.16553 12.14330 12.12307 12.10542
##  [97] 12.09092 12.07896 12.06842 12.05911 12.05087 12.04350 12.03684 12.03069
## [105] 12.02488 12.01922 12.01355 12.00767 12.00140 11.99457 11.98700 11.97906
## [113] 11.97129 11.96375 11.95650 11.94960 11.94311 11.93710 11.93163 11.92675
## [121] 11.92311 11.92111 11.92044 11.92080 11.92186 11.92333 11.92489 11.92625
## [129] 11.92708 11.92708 11.92595 11.92337 11.91879 11.91227 11.90442 11.89585
## [137] 11.88716 11.87897 11.87188 11.86651 11.86346 11.86114 11.85773 11.85347
## [145] 11.84859 11.84334 11.83794 11.83264 11.82768 11.82330 11.81973 11.81721
## [153] 11.81598 11.81628 11.81835 11.82243 11.82874 11.83754 11.84906 11.86354
## [161] 11.88215 11.90517 11.93154 11.96021 11.99010 12.02015 12.04929 12.07647
## [169] 12.10061 12.12578 12.15630 12.19151 12.23073 12.27329 12.31854 12.36580
## [177] 12.41440 12.46368 12.51296 12.56159 12.60889 12.65419 12.69684 12.73615
## [185] 12.77146 12.80211 12.82742 12.84673 12.86454 12.88498 12.90683 12.92884
## [193] 12.94980 12.96847 12.98362 12.99403 12.99847 12.99657 12.98923 12.97705
## [201] 12.96063 12.94060 12.91757 12.89214 12.86493 12.83655 12.80760 12.77871
## [209] 12.75048 12.72353 12.69847 12.67047 12.63522 12.59410 12.54846 12.49966
## [217] 12.44908 12.39807 12.34801 12.30025 12.25616 12.21711 12.18446 12.15316
## [225] 12.11780 12.07918 12.03810 11.99537 11.95179 11.90816 11.86528 11.82397
## [233] 11.78502 11.74924 11.71743 11.69039 11.66892 11.65340 11.64281 11.63594
## [241] 11.63156 11.62842 11.62532 11.62100 11.61426 11.60770 11.60450 11.60422
## [249] 11.60645 11.61076 11.61674 11.62394 11.63196 11.64038 11.64875 11.65667
## [257] 11.66371 11.66945 11.67346 11.67532 11.67544 11.67470 11.67338 11.67179
## [265] 11.67020 11.66891 11.66820 11.66836 11.66968 11.67245 11.67695 11.68349
## [273] 11.69099 11.69830 11.70558 11.71294 11.72054 11.72852 11.73701 11.74615
## [281] 11.75608 11.76740 11.78042 11.79483 11.81033 11.82663 11.84341 11.86037
## [289] 11.87722 11.89365 11.90935 11.92404 11.93739 11.95123 11.96730 11.98526
## [297] 12.00476 12.02544 12.04695 12.06893 12.09104 12.11291 12.13420 12.15455
## [305] 12.17361 12.19103 12.20645 12.22037 12.23355 12.24604 12.25788 12.26910
## [313] 12.27976 12.28990 12.29955 12.30876 12.31758 12.32603 12.33418 12.34205
## [321] 12.34970 12.35662 12.36243 12.36737 12.37165 12.37550 12.37915 12.38283
## [329] 12.38675 12.39114 12.39630 12.40224 12.40875 12.41565 12.42274 12.42982
## [337] 12.43671 12.44320 12.44911 12.45423 12.45838 12.46135 12.46301 12.46342
## [345] 12.46278 12.46126 12.45904 12.45629 12.45320 12.44994 12.44668 12.44361
## [353] 12.44091 12.43874 12.43730 12.43674 12.43640 12.43554 12.43430 12.43281
## [361] 12.43119 12.42957 12.42809 12.42686 12.42603 12.42537 12.42460 12.42374
## [369] 12.42280 12.42182 12.42081 12.41978 12.41877 12.41778 12.41685 12.41598
## [377] 12.41520 12.41461 12.41425 12.41409 12.41408 12.41418 12.41434 12.41453
## [385] 12.41471 12.41482
#assign fits to a vector
both_trendc <- fit_bothc

#extract y min and max for each
limits_bothc <- ggplot_build(extract_bothc)$data
## `geom_smooth()` using formula 'y ~ x'
limits_bothc <- as.data.frame(limits_bothc)
both_yminc <- limits_bothc$ymin
both_ymaxc <- limits_bothc$ymax

#reassign dataframes (just to be safe)
work_bothc <- wrfc_both

#fill in missing dates to smooth fits
work_bothc <- work_bothc %>% complete(date = seq(min(date), max(date), by = "1 day"))
date_vec_bothc <- work_bothc$date

#create a new smooth dataframe to layer
smooth_frame_bothc <- data.frame(date_vec_bothc, both_trendc, both_yminc, both_ymaxc)
#WRF C
#plot smooth frames
p_wrf_c <- plotly::plot_ly() %>%
  plotly::add_lines(x = ~date_vec_bothc, y = ~both_trendc,
                    data = smooth_frame_bothc,
                    hoverinfo = "text",
                    text = ~paste('</br> Date: ', date_vec_bothc,
                                  '</br> Median Log Copies: ', round(both_trendc, digits = 2)),
                    line = list(color = '#E7298A', size = 8, opacity = 0.65),
                    showlegend = FALSE) %>%
     layout(xaxis = list(range = c(mindate - 7, maxdate + 7))) %>% #buffer here
plotly::add_ribbons(x ~date_vec_bothc, ymin = ~both_yminc, ymax = ~both_ymaxc,
                    showlegend = FALSE,
                    opacity = 0.25,
                    hoverinfo = "text",
                    text = ~paste('</br> Date: ', date_vec_bothc, #leaving in case we want to change
                                  '</br> Max Log Copies: ', round(both_ymaxc, digits = 2),
                                  '</br> Min Log Copies: ', round(both_yminc, digits = 2)),
                    name = "",
                    fillcolor = '#E7298A',
                    line = list(color = '#E7298A')) %>%
                layout(yaxis = list(title = "Total Log10 SARS CoV-2 Copies", 
                                 showline = TRUE,
                                 automargin = TRUE)) %>%
                layout(xaxis = list(title = "Date")) %>%
                layout(title = "WRF C") %>%
  plotly::add_markers(x = ~date, y = ~log_total_copies_both,
                      data = wrfc_both,
                       hoverinfo = "text",
                       showlegend = FALSE,
                       text = ~paste('</br> Date: ', date, 
                                     '</br> Actual Log Copies: ', round(log_total_copies_both, digits = 2)),
                       marker = list(color = '#E7298A', size = 6, opacity = 0.65))

p_wrf_c
save(p_wrf_c, file = "./site_objects/wrf_c_year2.rda")

keeping in case

#save(wrfa_both, file = "./plotly_objs/wrfa_both.rda")
#save(wrfb_both, file = "./plotly_objs/wrfb_both.rda")
#save(wrfc_both, file = "./plotly_objs/wrfc_both.rda")
#save(date_vec_botha, file = "./plotly_objs/date_vec_botha.rda")
#save(date_vec_bothb, file = "./plotly_objs/date_vec_bothb.rda")
#save(date_vec_bothc, file = "./plotly_objs/date_vec_bothc.rda")
#save(both_ymina, file = "./plotly_objs/both_ymina.rda")
#save(both_ymaxa, file = "./plotly_objs/both_ymaxa.rda")

#save(both_yminb, file = "./plotly_objs/both_yminb.rda")
#save(both_ymaxb, file = "./plotly_objs/both_ymaxb.rda")

#save(both_yminc, file = "./plotly_objs/both_yminc.rda")
#save(both_ymaxc, file = "./plotly_objs/both_ymaxc.rda")